package com.zjitc.pigspring.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zjitc.pigspring.bean.po.EChartsVO;
import com.zjitc.pigspring.bean.po.Goods;
import com.zjitc.pigspring.bean.po.Seller;
import com.zjitc.pigspring.common.Codes;
import com.zjitc.pigspring.common.Result;
import com.zjitc.pigspring.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/seller")
public class SellerController {

    @Autowired
    private SellerService sellerService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private CollectService collectService;

    @Autowired
    private ShopService shopService;

    @Autowired
    private OrderService orderService;

    @GetMapping("/querySellerTop/{number}")
    public Result<List<EChartsVO>> getTop(@PathVariable Integer number){
        List<EChartsVO> list = sellerService.getTop(number);
        if (list==null){
            return Result.error(Codes.GET_INFO_FAILED);
        }else{
            return Result.success(list);
        }
    }

    @GetMapping("/getAll")
    public Result<List<Seller>> getAll() {
        List<Seller> sellers = sellerService.list();
        if (sellers != null) {
            return Result.success(sellers);
        } else {
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }

    @PostMapping("/addOne")
    public Result addSeller(@RequestBody Seller seller) {
        QueryWrapper<Seller> wrapper = new QueryWrapper<Seller>()
                .eq("username", seller.getName());
        Seller one = sellerService.getOne(wrapper);
        if (one != null && !one.getUsername().equals(seller.getUsername())) {
            return Result.error(Codes.USER_EXISTS);
        }
        boolean save = sellerService.save(seller);
        if (save) {
            return Result.success(Codes.ADD_YES);
        } else {
            return Result.error(Codes.ADD_NO);
        }
    }

    @Transactional
    @DeleteMapping("/deleteOne/{id}")
    public Result deleteSeller(@PathVariable Integer id) {
        boolean flag = sellerService.removeById(id);
        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<Goods>()
                .eq("seller_id", id);
        List<Goods> list = goodsService.list(goodsQueryWrapper);
        boolean flag1 = true;
        for (Goods goods : list) {
            if (goodsService.removeById(goods)) {
                flag1 = deleteData(goods.getId());
                if (!flag1) {
                    break;
                }
            }
        }
        if (flag1 && flag) {
            return Result.success(Codes.DELETE_YES);
        }

        return Result.error(Codes.DELETE_NO);

    }

    public boolean deleteData(Integer id) {
        boolean flag1 = shopService.deleteByGoodId(id);
        boolean flag2 = orderService.deleteByGoodId(id);
        boolean flag3 = collectService.deleteByGoodId(id);

        return (flag1 && flag2 && flag3);
    }

    @Transactional
    @PostMapping("/deleteAll")
    public Result deleteSellers(@RequestBody List<Integer> ids) {
        boolean flag = sellerService.removeBatchByIds(ids);
        boolean flag1 = true;
        for (Integer id : ids) {
            QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<Goods>()
                    .eq("seller_id", id);
            List<Goods> list = goodsService.list(goodsQueryWrapper);
            for (Goods goods : list) {
                if (goodsService.removeById(goods)) {
                    flag1 = deleteData(goods.getId());
                    if (!flag1) {
                        break;
                    }
                }
            }
            if (!flag1) {
                break;
            }

        }
        if (flag && flag1) {
            return Result.success(Codes.DELETE_YES);
        } else {
            return Result.error(Codes.DELETE_NO);
        }
    }

    @GetMapping("/selectOne/{id}")
    public Result<Seller> selectOneSeller(@PathVariable Integer id) {
        Seller seller = sellerService.getById(id);
        if (seller == null) {
            return Result.error(Codes.GET_INFO_FAILED);
        } else {
            return Result.success(seller);
        }
    }

    @PutMapping("/updateOne")
    public Result updateSeller(@RequestBody Seller seller) {
        QueryWrapper<Seller> wrapper = new QueryWrapper<Seller>()
                .eq("username", seller.getName());
        Seller one = sellerService.getOne(wrapper);
        if (one != null) {
            return Result.error(Codes.USER_EXISTS);
        }
        boolean flag = sellerService.updateById(seller);
        if (flag) {
            return Result.success(Codes.UPDATE_YES);
        } else {
            return Result.error(Codes.UPDATE_NO);
        }
    }

    @PostMapping("/conditionQuery")
    public Result<List<Seller>> querySeller(@RequestBody Seller seller) {
        QueryWrapper<Seller> wrapper = new QueryWrapper<Seller>()
                .eq(StringUtils.isNotBlank(seller.getName()), "name", seller.getName())
                .eq(StringUtils.isNotBlank(seller.getStatus()), "status", seller.getStatus());
        List<Seller> sellers = sellerService.list(wrapper);
        if (sellers != null) {
            return Result.success(sellers);
        } else {
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }


}
